/**
 * Created by Dinh Thi on 2/25/15.
 */
var DanhSachHoaDon = {
    XemDanhSach: function(button){
        var begin = $('#txtNgayBatDau');
        if (begin.val() == ''){
             QuanLyBanHangStatic.changeMessage('Thông báo', 'Vui lòng chọn <b>Ngày bắt đầu</b>');
             QuanLyBanHangStatic.showMessageBox(true, 3000);
             begin.effect('highlight', {color: '#ffd5ad'}, 600)
             .effect('highlight', {color: '#ffd5ad'}, 600);
             begin.focus();
             return;
        }

        var end = $('#txtNgayKetThuc');
        if (end.val() == ''){
            QuanLyBanHangStatic.changeMessage('Thông báo', 'Vui lòng chọn <b>Ngày kết thúc</b>');
            QuanLyBanHangStatic.showMessageBox(true, 3000);
            end.effect('highlight', {color: '#ffd5ad'}, 600)
                .effect('highlight', {color: '#ffd5ad'}, 600);
            end.focus();
            return;
        }


        button = $(button);
        button.prop('disabled', true);
        button.find('.fa-loading').show();

        $.post(QuanLyBanHangStatic.baseURL + 'HoaDon/DanhSachHoaDon', {begin: begin.val(), end: end.val()}, function(data){
            data = JSON.parse(data);

            DanhSachHoaDon.HienThiDanhSach(data);

            button.prop('disabled', false);
            button.find('.fa-loading').hide();
        });

    },
    XemTatCa: function(button){
        button = $(button);
        button.prop('disabled', true);
        button.find('.fa-loading').show();

        $.get(QuanLyBanHangStatic.baseURL + 'HoaDon/TatCaHoaDon', function(data){
            data = JSON.parse(data);

            DanhSachHoaDon.HienThiDanhSach(data);

            button.prop('disabled', false);
            button.find('.fa-loading').hide();
        });
    },
    HienThiDanhSach: function(data){
        console.log(data);
        //Da thanh toan
        var container = $('#container-thanhtoan');
        var defaultRow = container.parent().find('.default-row');

        var listThanhToan = data['DaThanhToan'];
        var sizeThanhToan = listThanhToan.length;
        container.html('');

        for (var i = 0; i < sizeThanhToan; i++){
            var row = defaultRow.clone();
            row.removeClass('default-row');
            row.attr('data-text', listThanhToan[i].id);
            row.find('.stt').html(i + 1);
            row.find('.mahoadon').html(listThanhToan[i].id);
            row.find('.thoigian').html(listThanhToan[i].thoigian);
            row.find('.trigia').html(QuanLyBanHangStatic.numberWithCommas(listThanhToan[i].trigia) + 'đ');
            row.find('.khachhang').html(listThanhToan[i].ten);
            row.find('.sodienthoai').html(listThanhToan[i].sodienthoai);
            row.find('.diachi').html(listThanhToan[i].diachi);

            row.find('.chitiethoadon').attr('data-text', listThanhToan[i].id);
            row.find('.huyhoadon').attr('data-text', listThanhToan[i].id);

            row.find('.inhoadon-link').attr('data-text', listThanhToan[i].id);
            row.find('.ingiaohang-link').attr('data-text', listThanhToan[i].id);
/*

            row.find('.inhoadon-link').attr('href', QuanLyBanHangStatic.baseURL + 'Print/HoaDonBanHang/' + listThanhToan[i].id);
            row.find('.ingiaohang-link').attr('href', QuanLyBanHangStatic.baseURL + 'Print/GiaoHang/' + listThanhToan[i].id);
*/

            row.appendTo(container).show();
        }

        container.effect('highlight', {color: '#ffd5ad'}, 600)
            .effect('highlight', {color: '#ffd5ad'}, 600);




        //Chua thanh toan
        container = $('#container-chuathanhtoan');
        defaultRow = container.parent().find('.default-row');

        var listChuaThanhToan = data['ChuaThanhToan'];
        var sizeChuaThanhToan = listChuaThanhToan.length;
        container.html('');

        for (i = 0; i < sizeChuaThanhToan; i++){
            row = defaultRow.clone();
            row.removeClass('default-row');
            row.attr('data-text', listChuaThanhToan[i].id);
            row.find('.stt').html(i + 1);
            row.find('.mahoadon').html(listChuaThanhToan[i].id);
            row.find('.thoigian').html(listChuaThanhToan[i].thoigian);
            row.find('.trigia').html(QuanLyBanHangStatic.numberWithCommas(listChuaThanhToan[i].trigia) + 'đ');

            row.find('.thanhtoanhoadon').attr('href', QuanLyBanHangStatic.baseURL + 'HoaDon/ThanhToan/' + listChuaThanhToan[i].id);

            row.find('.chitiethoadon').attr('data-text', listChuaThanhToan[i].id);
            row.find('.huyhoadon').attr('data-text', listChuaThanhToan[i].id);

            row.appendTo(container).show();
        }

        container.effect('highlight', {color: '#ffd5ad'}, 600)
            .effect('highlight', {color: '#ffd5ad'}, 600);

    },
    XemChiTietHoaDon: function(obj){
        QuanLyBanHangStatic.changeMessage('Thông báo', 'Đang tải thông tin...', 'fa-spin fa-refresh');
        QuanLyBanHangStatic.showMessageBox(false);

        obj = $(obj);
        var id = obj.attr('data-text');

        $.get(QuanLyBanHangStatic.baseURL + 'HoaDon/ChiTietHoaDon/' + id, function(data){
            data = JSON.parse(data);
            console.log(data);
            QuanLyBanHangStatic.showMessageBox(true, 1);

            var size = data.length;
            var container = $('#container-chitiet');
            var defaultRow = container.parent().find('.default-row');

            container.html('');
            for (var i = 0; i < size; i++){
                var row = defaultRow.clone();
                row.removeClass('default-row');
                row.find('.stt').html(i + 1);
                row.find('.tenmathang').html(data[i].ten);
                row.find('.soluong').html(data[i].soluong);
                row.find('.donvitinh').html(data[i].donvitinh);
                row.find('.giaban').html(QuanLyBanHangStatic.numberWithCommas(data[i].giaban) + 'đ');
                row.find('.thanhtien').html(QuanLyBanHangStatic.numberWithCommas(data[i].thanhtien) + 'đ');


                row.appendTo(container).show();
            }



            $('html, body').animate({
                scrollTop: container.offset().top
            }, 1000);

            setTimeout(function(){
                container.effect('highlight', {color: '#ffd5ad'}, 600)
                    .effect('highlight', {color: '#ffd5ad'}, 600);
            }, 500);

        });
    },
    InHoaDon: function(obj){
        var left = (screen.width/2)-(450);
        var top = (screen.height/2)-(250);

        var id = $(obj).attr('data-text');
        window.open(QuanLyBanHangStatic.baseURL + 'Print/HoaDonBanHang/' + id,'winname','directories=no,titlebar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no,width=900,height=500,top='+ top +',left='+ left);
    },
    PhieuGiaoHang: function(obj){

        var left = (screen.width/2)-(450);
        var top = (screen.height/2)-(250);
        var id = $(obj).attr('data-text');
        window.open(QuanLyBanHangStatic.baseURL + 'Print/GiaoHang/' + id,'winname','directories=no,titlebar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no,width=900,height=500,top='+ top +',left='+ left);
    },
    HuyHoaDon: function(obj){
        QuanLyBanHangStatic.changeMessage('Thông báo', 'Đang hủy hóa đơn...', 'fa-spin fa-refresh');
        QuanLyBanHangStatic.showMessageBox(false);

        obj = $(obj);
        var id = obj.attr('data-text');

        $.get(QuanLyBanHangStatic.baseURL + 'HoaDon/HuyHoaDon/' + id, function(){
            QuanLyBanHangStatic.changeMessage('Thông báo', 'Hủy hóa đơn hoàn tất', 'fa-trash');
            QuanLyBanHangStatic.showMessageBox(true, 3000);
            obj.parent().parent().remove();
        });
    }

};